#include"comm.hpp"

using namespace std;

int main()
{
    key_t key=creat_key();
    cout<<"key:"<<key<<endl;
    //创建共享内存
    int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0666);
    if(shmid<0)
    {
        log()<<"error"<<strerror(errno)<<endl;
        exit(2);
    }
    log()<<"shared memory sucessed shmid:"<<shmid<<endl;
    //进程关联共享内存
    char* str=(char*)shmat(shmid,nullptr,0);
    if(str==(char*)-1)
    {
        log()<<"attach failed"<<endl;
        exit(3);
    }
    log()<<"attach successed shmid:"<<shmid<<endl;

    //使用
    //读出
    while(1)
    {
        cout<<"服务器端读出#:";
        cout<<str<<endl;
        sleep(1);
    }
    //进程去关联共享内存
    shmdt(str);
    log()<<"detach sucess"<<endl;
    //删除共享内存
    shmctl(shmid,IPC_RMID,nullptr);
    return 0;
}
